<?php

class User {

	static public function is_logged_in () {
		return isset( $_SESSION['userid']);
	}
	
	static public function get_id () {
		$user_id = isset( $_SESSION['userid']) ? $_SESSION['userid'] : 0;  
		return $user_id;
	}
	
	static public function get_name () {
		return $_SESSION['name'];
	}
	
	
	static public function get_username () {
		return $_SESSION['username'];
	}
	
	static public function get_email () {
		return $_SESSION['email'];
	}
	
	static public function get_prop ($prop_name) {
		/*
		$ret = '';
		if( is_array( $_SESSION[$prop_name] ) && isset( $_SESSION[$prop_name]['id'] ) ) $ret = $_SESSION[$prop_name]['id'];
		if( $ret == '' && isset( $_SESSION[$prop_name] ) ) $ret = $_SESSION[$prop_name];
		return $ret;
		*/
		global $db;

		$props = array( 'userid' => 'id', 'name' => 'nev', 'username' => 'bejnev', 'email' => 'email', 'telefon' => 'telefon', 'varos' => 'varos', 'regio' => 'regio', 'rovat' => 'altkategoria', 'weblap' => 'weblap' );	
		
		$prop = $props[$prop_name];
		 
		$db->sql = "SELECT $prop FROM users WHERE id='".$_SESSION['userid']."'";				
		$db->query();
		$rs = mysql_fetch_row( $db->rs);
		return $rs[0];
		
	}
	
	static public function set_props ($prop_names) {
		global $db;
		
		//debug($prop_names,'prop_names');
		
		foreach ($prop_names as $prop_name => $prop_value) {
			$props = array( 'userid' => 'id', 'name' => 'nev', 'username' => 'bejnev', 'email' => 'email', 'telefon' => 'telefon', 'varos' => 'varos', 'regio' => 'regio', 'rovat' => 'altkategoria', 'weblap' => 'weblap' );		
			$prop = $props[$prop_name];
			$db->sql = "UPDATE users SET $prop='$prop_value' WHERE id='".$_SESSION['userid']."'";				
			$db->query();
			$_SESSION[$prop_name] = $prop_value;
		}

		//debug($_SESSION,'session');
	}
	
	static public function login ($username, $password) {
		
		$success = false;
		$errors  = array();
		
		if (count( $errors) < 1) {
			
			$success = true;
			
			$_SESSION['userid'] = 1;
			$_SESSION['username'] = 'admin';
			$_SESSION['email'] = 'admin@admin.hu';
		}
		
		return array ('success' => $success, 'errors' => $errors);
	}
}

?>
